package collection.set;

import java.util.TreeSet;

public class TreeSetCompareFun {
    public static void main(String[] args) {
        //方法一：自定义类实现Comparable接口，重写里面的compareTo方法来指定比较规则
        Student s1 = new Student("zhangsan", 22);
        Student s2 = new Student("lisi", 18);
        Student s3 = new Student("wangwu", 62);
        Student s4 = new Student("zhaoliu", 34);
        Student s5 = new Student("iwen", 22);
        Student s6 = new Student("enid", 22);
        TreeSet<Student> set = new TreeSet<>();
        set.add(s1);
        set.add(s2);
        set.add(s3);
        set.add(s4);
        set.add(s5);
        set.add(s6);
        for (Student student : set) {
            System.out.println(student);
        }
        System.out.println("------------------------------------------");
//--------------------------------------------------------------------------------------
        //方法二：通过调用TreeSet有参构造器，可设置comparator对象
        Teacher t1 = new Teacher("zhangsan", 22,3600.5);
        Teacher t2 = new Teacher("lisi", 18,4600.5);
        Teacher t3 = new Teacher("wangwu", 62,4300);
        Teacher t4 = new Teacher("zhaoliu", 34,5000);
        //工资升序
        TreeSet<Teacher> teachers = new TreeSet<>((o1, o2) -> Double.compare(o1.getSalary(), o2.getSalary()));
        teachers.add(t1);
        teachers.add(t2);
        teachers.add(t3);
        teachers.add(t4);
        for (Teacher teacher : teachers) {
            System.out.println(teacher);
        }
    }
}
